﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AcademicScheduleControl.ascx.cs" Inherits="AcademicPlanner.Presentation.Views.Shared.AcademicScheduleControl" %>
<%@ Import Namespace="AcademicPlanner.Framework.Helpers" %>
<%@ Import Namespace="AcademicPlanner.Presentation" %>
<%@ Import Namespace="AcademicPlanner.Presentation.Views.Shared" %>
<%@ Import Namespace="AcademicPlanner.Domain.Models" %>

<%
var vCoursesToIgnore = new List<Course>();
var vRegistrations = Model.AcademicSchedule.Registrations.ToList();
var vRegCategories = new List<CourseCategory>();
foreach (var vReg in vRegistrations)
{
	if (!vRegCategories.Contains(vReg.Course.Category))
		vRegCategories.Add(vReg.Course.Category);	
}
 %>

<% if (!Model.IsSub)
   { %>
		<div class="academic-schedule-control" id="<%= Model.Id %>"
				cSelfUrl="<%= Url.Action("AcademicScheduleControl", "Controls", 
												new { pAcademicScheduleId = Model.AcademicSchedule.EntityId }) %>"
				cSaveUrl="<%= Url.Action("SaveRegistrations", "Schedules", 
												new { pId = Model.AcademicSchedule.EntityId }) %>">
			<%= Html.Encode(Tr("Click on a Course on the right then Drag it!")) %>
			<table class="no-borders">
				<tr>
					<td class="academic-schedule-legend-missing-prequisites">
						<%= Html.Encode(Tr("cannot be dropped due to missing prequisites")) %>
					</td>
					<td class="academic-schedule-legend-not-commonly-offered-in">
						<%= Html.Encode(Tr("can be dropped but is not commonly offered in term")) %>
					</td>
					<td class="academic-schedule-legend-acceptable">
						<%= Html.Encode(Tr("can be dropped")) %>
					</td>
				</tr>
			</table>
			<span class="strong"><%= Html.Encode(Model.AcademicSchedule.Program.Name) %>: <%= Model.AcademicSchedule.GetTotalCredits() %>/<%= Model.AcademicSchedule.Program.MinimumCredits %></span>
			<span class="right yui-skin-sam">
				<%= Html.PushButton("saveAcademicSchedule", Tr("Save"), "gAcademicScheduleControlCtrl.finishSaveButton", "") %>
				<%= Html.NBS(2) %>
				<%= Html.PushButton("reloadAcademicSchedule", Tr("Reload"), "gAcademicScheduleControlCtrl.finishReloadButton", "") %>
			</span>
			<%= Html.ClearBreak() %>
			<%= Html.Break() %>
<% } %>

<div class="academic-schedule-control-bd yui-skin-sam">	
	<div class="registrations left">
		<table>
			<tr>
				<th></th>
				<% foreach (var vCat in vRegCategories)
				{ %>
					<th><%= Html.Encode(vCat.Name) %></th>
					<%} %>
				<th></th>
				</tr>
			<% var vLastYear = Model.AcademicSchedule.StartYear + 6; %>
			<%for (var vYear = Model.AcademicSchedule.StartYear; vYear < vLastYear; vYear++)
			{ %>
				<% var vI = 0; %>
				<%foreach (var vTerm in Model.AcademicSchedule.Plan.Terms.ToList())
				{ %>
					<%
						var vPeriodRegs = vRegistrations.Where(pReg => (pReg.Term.EntityId == vTerm.EntityId
																		&& pReg.Year == vYear));
					 %>
					<tr style="background:#<%= RegistrationBgColors[vI++%6] %>">
						<td><div class="registration-period strong" cYear="<%= vYear.ToString() %>" cTermId="<%= vTerm.EntityId.ToString() %>">
							<%= vTerm.Name + " " + vYear %>
						</div></td>
						<% foreach (var vCat in vRegCategories)
						{ %>
							<%
								var vCatPeriodRegs = vPeriodRegs.Where(pReg => (pReg.Course.Category.EntityId == vCat.EntityId));
							 %>
							<td>
								<div class="registration-slot" id="<%= Model.Id %>RegistrationSlotT<%= vTerm.EntityId.ToString() + "Y" + vYear.ToString() + "C" + vCat.EntityId.ToString() %>">
								 <% foreach (var vReg in vCatPeriodRegs)
								 { %>
									<%
										var vCourse = vReg.Course;
										var vPrereqIds = new List<string>();
										var vPrereqNames = new List<string>();
										var vCmTermOfferedIds = new List<string>();

										foreach (var vPrereq in vCourse.Prerequisites.ToList())
										{
											vPrereqIds.Add(vPrereq.EntityId.ToString());
											vPrereqNames.Add(vPrereq.ShortName);
										}
										foreach (var vCmTerm in vCourse.CommonlyOfferedInTerms.ToList())
										{
											vCmTermOfferedIds.Add(vCmTerm.EntityId.ToString());
										}
										vCoursesToIgnore.Add(vCourse);
									 %>
									<div class="course"
										 id="<%= Model.Id %>Course<%= vCourse.EntityId.ToString() %>"
										 cId="<%= vCourse.EntityId.ToString() %>"
										 cPrequisiteIds="<%= vPrereqIds.Count > 0 ? string.Join(",", vPrereqIds.ToArray()) + "," : "" %>"
										 cCommonTermIds="<%= vCmTermOfferedIds.Count > 0 ? string.Join(",", vCmTermOfferedIds.ToArray()) + "," : "" %>"
										 cCategoryName="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Category.Name) %>"
										 cCategoryId="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Category.EntityId.ToString()) %>"
										 style="background:<%= Model.AcademicSchedule.Program.IsCourseRequired(vCourse) ? "#FF9640" : Model.AcademicSchedule.Program.IsCourseRecommended(vCourse) ? "#FFB273" : "#e0e0e0" %>">
										<span class="remove-course-link right">
											<%= HtmlHelperExtensions.Image(Html, Url.Icon("remove.png"), Tr("Remove"), new { @class = "link small-icon", onclick = "gAcademicScheduleControlCtrl.onRemoveCourseClick(event, this)" })%>
										</span>
										<%= Html.Encode(vCourse.ShortName) %>
										<span class="course-info-tipper right" cTip="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Name) + "<br/>" %>Credits: <%= vCourse.Credits.ToString() + "<br/>" %>Prerequisites: <%= SecurityHelper.EscapeDoubleQuotes(string.Join(",", vPrereqNames.ToArray())) %>">
											<%= HtmlHelperExtensions.Image(Html, Url.Icon("info.png"), "", new { @class = "tipper small-icon" })%>	
										</span>
									</div>
									<%} %>
								</div>
							</td>
							<%} %>
						<td><div class="registration-slot" id="<%= Model.Id %>RegistrationSlotT<%= vTerm.EntityId.ToString() + "Y" + vYear.ToString() %>">
						<%= Html.NBS() %>
						</div></td>
					</tr>
					<%} %>
				<%}%>
		</table>
	</div>
	<div class="courses left-separator thick-border right">
		<ul>
		<%foreach (var vCourse in Model.AcademicSchedule.Plan.Courses.ToList())
		{ %>
			<li>
				<div class="course-slot" id="<%= Model.Id %>CourseSlot<%= vCourse.EntityId.ToString() %>">
				<% if (!vCoursesToIgnore.Contains(vCourse))
				{ %>
					<%
						var vPrereqIds = new List<string>();
						var vPrereqNames = new List<string>();
						var vCmTermOfferedIds = new List<string>();

						foreach (var vPrereq in vCourse.Prerequisites.ToList())
						{
							vPrereqIds.Add(vPrereq.EntityId.ToString());
							vPrereqNames.Add(vPrereq.ShortName);
						}
						foreach (var vCmTerm in vCourse.CommonlyOfferedInTerms.ToList())
						{
							vCmTermOfferedIds.Add(vCmTerm.EntityId.ToString());
						}
					%>
						<div class="course"
							 id="<%= Model.Id %>Course<%= vCourse.EntityId.ToString() %>"
							 cId="<%= vCourse.EntityId.ToString() %>"
							 cPrequisiteIds="<%= vPrereqIds.Count > 0 ? string.Join(",", vPrereqIds.ToArray()) + "," : "" %>"
							 cCommonTermIds="<%= vCmTermOfferedIds.Count > 0 ? string.Join(",", vCmTermOfferedIds.ToArray()) + "," : "" %>"
							 cCategoryName="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Category.Name) %>"
							 cCategoryId="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Category.EntityId.ToString()) %>"
							 style="background:<%= Model.AcademicSchedule.Program.IsCourseRequired(vCourse) ? "#FF9640" : Model.AcademicSchedule.Program.IsCourseRecommended(vCourse) ? "#FFB273" : "#e0e0e0" %>">
							<span class="remove-course-link right hidden">
								<%= HtmlHelperExtensions.Image(Html, Url.Icon("remove.png"), Tr("Remove"), new { @class = "link small-icon", onclick = "gAcademicScheduleControlCtrl.onRemoveCourseClick(event, this)" })%>
							</span>
							<%= Html.Encode(vCourse.ShortName) %>
							<span class="course-info-tipper right" cTip="<%= SecurityHelper.EscapeDoubleQuotes(vCourse.Name) + "<br/>" %>Credits: <%= vCourse.Credits.ToString() + "<br/>" %>Prerequisites: <%= SecurityHelper.EscapeDoubleQuotes(string.Join(",", vPrereqNames.ToArray())) %>">
								<%= HtmlHelperExtensions.Image(Html, Url.Icon("info.png"), "", new { @class = "tipper small-icon" })%>	
							</span>
						</div>
					<%} %>
				</div>
			</li>
			<%}%>
		<% if (Model.AcademicSchedule.Plan.Courses.ToList().Count <= 0)
		{ %>
			<li>
				<%= Html.ActionLink(Tr("Create courses first!"), "List", "Courses", new { pId = Model.AcademicSchedule.Plan.EntityId }, new { }) %>
			</li>
			<%} %>
		</ul>
		<%= Html.NBS() %>
	</div>
	<%= Html.ClearBreak() %>
</div>
<script type="text/javascript">
	gDomReadyQueue.push('gAcademicScheduleControlCtrl.init("<%= Model.Id %>");');
</script>

<% if (!Model.IsSub)
   { %>
			<%= Html.Encode(Tr("Credits: ")) %><span class="strong"><%= Model.AcademicSchedule.GetTotalCredits() %>/<%= Model.AcademicSchedule.Program.MinimumCredits %></span>
			<span class="right yui-skin-sam">
				<%= Html.PushButton("saveAcademicSchedule", Tr("Save"), "gAcademicScheduleControlCtrl.finishSaveButton", "") %>
				<%= Html.NBS(2) %>
				<%= Html.PushButton("reloadAcademicSchedule", Tr("Reload"), "gAcademicScheduleControlCtrl.finishReloadButton", "") %>
			</span>
			<%= Html.ClearBreak() %>
		</div>
<% } %>